package com.dingtalk.demo.biz.impl;

import com.dingtalk.demo.biz.BlogBiz;
import com.dingtalk.demo.biz.common.BizResult;
import com.dingtalk.demo.biz.common.Transform;
import com.dingtalk.demo.biz.common.TransformHelper;
import com.dingtalk.demo.biz.dao.BlogDAO;
import com.dingtalk.demo.biz.dao.UserDAO;
import com.dingtalk.demo.biz.dao.model.BlogDO;
import com.dingtalk.demo.biz.dao.model.UserDO;
import com.dingtalk.demo.biz.query.BlogQuery;
import com.dingtalk.demo.biz.vo.BlogVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by well on 17/7/17.
 */
@Service
public class BlogBizImpl implements BlogBiz {

    private final static Logger logger = LoggerFactory.getLogger(BlogBizImpl.class);

    @Autowired
    private BlogDAO blogDAO;

    @Autowired
    private UserDAO userDAO;


    public BizResult detail(long id) {
        BizResult bizResult = new BizResult();
        try{
            BlogDO blogDO = blogDAO.selectById(id);
            if(blogDO == null){
                return bizResult;
            }
            bizResult.data.put("blogDO", blogDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Blog error",e);
        }
        return bizResult;
    }

    public BizResult list(BlogQuery blogQuery) {
        BizResult bizResult = new BizResult();
        try {
            int totalCount = blogDAO.countForPage(blogQuery);
            blogQuery.setTotalCount(totalCount);
            List<BlogDO> blogList = blogDAO.selectForPage(blogQuery);

            Set<Long> userIdSet = TransformHelper.transform2Set(blogList, new Transform<BlogDO, Long>() {

                public Long transform(BlogDO object) {
                    return object.getUserId();
                }
            });
            List<Long> userIdList = new ArrayList(userIdSet);

            Map<Long, UserDO> userDOMap = new HashMap<Long, UserDO>(50);
            if(userIdList.size()>0){
                List<UserDO> userDOList = userDAO.selectByIds(userIdList);
                for (UserDO userDO : userDOList) {
                    userDOMap.put(userDO.getUserId(), userDO);
                }
            }

            List<BlogVO> blogVOList = new ArrayList<BlogVO>(blogList.size());
            for (BlogDO blogDO : blogList) {
                BlogVO blogVO = new BlogVO();
                blogVO.setBlogDO(blogDO);
                blogVO.setUserDO(userDOMap.get(blogDO.getUserId()));
                blogVOList.add(blogVO);
            }
            bizResult.data.put("blogVOList", blogVOList);

            BlogQuery hotQuery = new BlogQuery();
            List<BlogDO> hotBlogList = blogDAO.selectHotForPage(hotQuery);
            bizResult.data.put("hotBlogList", hotBlogList);
            bizResult.data.put("query", blogQuery);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("view Blog list error", e);
        }
        return bizResult;
    }

    public BizResult delete(long id) {
        BizResult bizResult = new BizResult();
        try {
            blogDAO.delById(id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("delete blog error", e);
        }
        return bizResult;
    }

    public BizResult create(BlogDO blogDO) {
        BizResult bizResult = new BizResult();
        try {
            long id = blogDAO.insert(blogDO);
            bizResult.data.put("id", id);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("create Blog error", e);
        }
        return bizResult;
    }


    public BizResult update(BlogDO blogDO) {
        BizResult bizResult = new BizResult();
        try {
            blogDAO.updateByIdSelective(blogDO);
            bizResult.success = true;
        } catch (Exception e) {
            logger.error("update Blog error", e);
        }
        return bizResult;
    }


    public BizResult queryForEdit(long id) {
        BizResult bizResult = new BizResult();
        try{
            BlogDO blogDO = blogDAO.selectById(id);
            bizResult.data.put("blogDO", blogDO);
            bizResult.success = true;
        }catch(Exception e){
            logger.error("query Blog error",e);
        }
        return bizResult;
    }

}
